package com.yihr.myshiro.realm;

import org.apache.shiro.authc.*;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class UserRealm extends AuthorizingRealm {


    // 加盐，用 HashedCredentialsMatcher
    @Override
    public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
        super.setCredentialsMatcher(credentialsMatcher);
    }

    //    获取身份验证信息（doGetAuthenticationInfo）
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        UsernamePasswordToken upToken = (UsernamePasswordToken) authenticationToken;
        String username = upToken.getUsername();
        String pw = String.valueOf(upToken.getPassword());


        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo();
//        info.setCredentialsSalt("ss");

        return null;
    }


    //  获取授权信息（doGetAuthorizationInfo）
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        principalCollection.getPrimaryPrincipal();

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.addRole("admin");
        info.addStringPermission("admin:view");


        return null;
    }


}
